<?php 
require_once('include/My/DbHelper.php');
require_once('include/My/Alert.php');
require_once('include/My/Activity.php');

class AccountsController extends SugarController
{
    public function action_saveAlert()
    {
        $obj   = new stdClass();
        $data  = $_POST;
        $alert = new Alert();

        if ($alert->isActive($data['record'])) {
            $alert->update($data);
        } else {
            $alert->createNew($data);
        }
        
        $obj->date = $data['date'];
        $obj->msg  = 'Powiadomienie zostało dodane';
        echo json_encode($obj);
        exit;
    }
    
    public function action_activity()
    {
        $act = new Activity();
        $act->checkActivity('Accounts', true);
        $act->checkActivity('Contacts', true);
        exit;
    }
    
    public function action_activitydb()
    {
        $act = new Activity();
        $act->checkActivity('Accounts', true, true);
        $act->checkActivity('Contacts', true, true);
        exit;
    }
    
    public function action_alerts()
    {
        $act = new Alert();
        $act->sendAll();
        exit;
    }
    
    public function action_getAlertForm()
    {
        $obj       = new stdClass();
        $smarty    = new Sugar_Smarty();
        $obj->view =  $smarty->fetch('modules/Accounts/alertForm.tpl');
        
        echo json_encode($obj);
        exit;
    }
}